package com.wsoft.dc.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.service.RedisService;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.dc.entity.DcBusinessReadEntity;
import com.wsoft.dc.enums.ReadTypeEnum;
import com.wsoft.dc.manager.DcBusinessReadManager;
import com.wsoft.dc.mapper.DcBusinessReadMapper;
import com.wsoft.dc.query.dc.DcListQuery;
import com.wsoft.dc.query.read.BusinessReadQuery;
import com.wsoft.dc.query.read.ReadAllQuery;
import com.wsoft.dc.service.IDcBusinessReadService;
import com.wsoft.dc.service.IDcService;
import com.wsoft.entity.BaseConfigVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 待办阅读状态 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年12月17日 16:17:47
 */
@Service
public class DcBusinessReadServiceImpl extends ServiceImpl<DcBusinessReadMapper, DcBusinessReadEntity> implements IDcBusinessReadService {

    @Resource
    private DcBusinessReadManager readManager;
    @Resource
    private RedisService redisService;
    @Resource
    private IDcService dcService;

    /**
     * 待办已读
     * @param query
     * @param type 已读/未读
     * @return
     */
    @Override
    public boolean read(BusinessReadQuery query, Integer type) {
        Long memberId = LoginUserUtil.getUserInfo().getLoginUserVO().getId();
        DcBusinessReadEntity info = readManager.getReadInfo(query.getConfigId(), query.getTodoId(),memberId);
        if(type.equals(ReadTypeEnum.READ.getType())){
            //置为已读
            if(null == info){
                DcBusinessReadEntity entity = new DcBusinessReadEntity();
                entity.setReadFlag(ReadTypeEnum.READ.getType());
                entity.setConfigId(query.getConfigId());
                entity.setKey(query.getTodoId());
                entity.setMemberId(memberId);
                boolean b = readManager.save(entity);
                if(b){
                    //已读
                    redisService.sAdd(RedisKeyConstant.Config.MEMBER_READ + memberId + StrUtil.COLON + query.getConfigId(), query.getTodoId());
                }
                return b;
            }else if(Objects.equals(info.getReadFlag(), ReadTypeEnum.UNREAD.getType())){
                info.setReadFlag(ReadTypeEnum.READ.getType());
                boolean b = readManager.updateById(info);
                if(b){
                    //已读
                    redisService.sAdd(RedisKeyConstant.Config.MEMBER_READ + memberId + StrUtil.COLON + query.getConfigId(), query.getTodoId());
                }
                return b;
            }else{
                return true;
            }

        }else if(type.equals(ReadTypeEnum.UNREAD.getType())){
            //置为未读
            if(null == info){
                DcBusinessReadEntity entity = new DcBusinessReadEntity();
                entity.setReadFlag(ReadTypeEnum.UNREAD.getType());
                entity.setConfigId(query.getConfigId());
                entity.setKey(query.getTodoId());
                entity.setMemberId(memberId);
                boolean b = readManager.save(entity);
                if(b){
                    redisService.sRemove(RedisKeyConstant.Config.MEMBER_READ + memberId + StrUtil.COLON + query.getConfigId(), query.getTodoId());
                }
                return b;
            }else if(Objects.equals(info.getReadFlag(), ReadTypeEnum.READ.getType())){
                info.setReadFlag(ReadTypeEnum.UNREAD.getType());
                boolean b = readManager.updateById(info);
                if(b){
                    redisService.sRemove(RedisKeyConstant.Config.MEMBER_READ + memberId + StrUtil.COLON + query.getConfigId(), query.getTodoId());
                }
                return b;
            }else{
                return true;
            }
        }
        return false;
    }



    /**
     * 一键已读
     * @param query
     * @return
     */
    @Override
    public boolean readAll(ReadAllQuery param) {
        DcListQuery query  = new DcListQuery();
        if(null != param.getModuleId()){
            query.setModuleId(Collections.singletonList(param.getModuleId()));
        }
        query.setIsRead(ReadTypeEnum.UNREAD.getType());
        query.setParamMap(new HashMap<>(1));
        Map<String, Object> map = dcService.dcList(query, false);
        List<BaseConfigVO> dataList =JSONArray.parseArray(JSONObject.toJSONString(map.get("dataList")),BaseConfigVO.class);
        if(CollUtil.isEmpty(dataList)){
            return true;
        }
        Long memberId = LoginUserUtil.getUserInfo().getLoginUserVO().getId();
        //清空之前是未读的数据, 避免重复添加(可能是已读转未读的)
        readManager.removeUnRead(memberId);

        List<DcBusinessReadEntity> addList = new ArrayList<>();

        for (BaseConfigVO baseConfigVO : dataList) {
            DcBusinessReadEntity entity = new DcBusinessReadEntity();
            entity.setConfigId(baseConfigVO.getConfigId());
            entity.setKey(baseConfigVO.getTodoId());
            entity.setMemberId(memberId);
            entity.setReadTime(LocalDateTime.now());
            entity.setReadFlag(ReadTypeEnum.READ.getType());
            addList.add(entity);
        }
        boolean b = readManager.saveBatch(addList);
        if(b){
            for (DcBusinessReadEntity item : addList) {
                redisService.sAdd(RedisKeyConstant.Config.MEMBER_READ + memberId + StrUtil.COLON + item.getConfigId(), item.getKey());
            }
        }
        return b;
    }
}
